from django.http import HttpResponse, request
from django.shortcuts import render

# Create your views here.
from django.views import View
import json


class KaoShi(View):
    def post(self, request):
        # key = self.request.POST.get('key')
        # value = self.request.POST.get('value')
        # print(key, value)


        json_bytes = self.request.body

        json_str = json_bytes.decode()


        dict = json.loads(json_str)
        key=dict.get('key')
        value=dict.get('value')
        print(key,value)
        self.response = HttpResponse('key:value添加成功')
        self.response.set_cookie(key, value, max_age=3600)



        return self.response

    # cookie?key=python
    def get(self, request):
        key = self.request.GET.get('key')  #python
        # value=self.request.GET.get('value') #cookie python:py37

        value= request.COOKIES.get(key)

        alldate = key + ':' + value
        return HttpResponse(alldate)

# def post(request):
#     # key = self.request.POST.get('key')
#     # value = self.request.POST.get('value')
#     # print(key, value)
#
#
#     json_bytes =request.body
#
#     json_str = json_bytes.decode()
#
#
#     dict = json.loads(json_str)
#     key=dict.get('key')
#     value=dict.get('value')
#     print(key,value)
#
#     # response.set_cookie(key, value, max_age=3600)
#     return HttpResponse('key:value添加成功')